﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using WebManager;


namespace BaoJian.WebManager.Function.ExProduct
{
    public partial class EditExchangeProduct : System.Web.UI.Page
    {
        /// <summary>
        /// 产品ID
        /// </summary>
        private string oid = null;
        /// <summary>
        /// 是否新建
        /// </summary>
        bool isNew = true;
        /// <summary>
        /// 栏目ID
        /// </summary>
        private string ColumnID = null;

        private LYQ.BLL.Product productBll = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            oid = HttpContext.Current.Request.QueryString["OID"];
            ColumnID = HttpContext.Current.Request.QueryString["ColumnID"];

            if (!Page.IsPostBack)
            {
                //产品名称
                productBll = new LYQ.BLL.Product();
                drpPName.DataSource = productBll.GetList("");
                drpPName.DataBind();
                ListItem item = new ListItem("请选择", "-1");
                drpPName.Items.Insert(0, item);


                LYQ.BLL.ExchangeProduct bll = new LYQ.BLL.ExchangeProduct();
                LYQ.Model.ExchangeProduct model;//= bll.GetModel(oid);

                txtSID.Text = model.SID.ToString();

                item = drpPName.Items.FindByText(model.PName);
                item.Selected = true;

                item = catalog.Items.FindByValue(model.PCatalog.ToString());
                item.Selected = true;
                txtRegTime.Value = model.RegDate.ToString("yyyy-MM-dd");
                txtDeadline.Value = model.Deadline.ToString("yyyy-MM-dd");
                txtPrice.Text = model.Price.ToString();
                txtTotalNum.Text = model.TotalNum.ToString();
                txtProduceDate.Value = model.ProduceDate.ToString("yyyy-MM-dd");
                txtExpireDate.Value = model.ExpireDate.ToString("yyyy-MM-dd");
                txtExReason.Text = model.ExReason;
                Image1.ImageUrl = model.ImagePath;                

                
            }

        }

        protected void Save_Click(object sender, EventArgs e)
        {
         


            LYQ.BLL.ExchangeProduct bll = new LYQ.BLL.ExchangeProduct();
            LYQ.Model.ExchangeProduct model = null;
           
            model;// = bll.GetModel(oid);
            model.SID = Convert.ToInt32(txtSID.Text.Trim());           

            model.PName = drpPName.SelectedItem.Text;
            model.ImagePath = Image1.ImageUrl;
            model.ProduceDate = Convert.ToDateTime(txtProduceDate.Value);
            model.ExpireDate = Convert.ToDateTime(txtExpireDate.Value);

            model.TotalNum = Convert.ToInt32(txtTotalNum.Text);
            model.RegDate = DateTime.Parse(txtRegTime.Value);
            model.Deadline = DateTime.Parse(txtDeadline.Value);
            model.ExReason = txtExReason.Text;
            model.Price = Convert.ToInt32(txtPrice.Text);
            model.PCatalog = new Guid(catalog.SelectedItem.Value);
            model.PCatalogName = catalog.SelectedItem.Text;
            model.Description = "";
            model.Days = 0;
            model.Publish = false;


       
            bll.Update(model);

            string url = URLUtility.UrlBase + "/WebManager/Function/ExProduct/exchange/ListExchangeProducts.aspx?oid=" + ColumnID;
            Page.Response.Redirect(url);

        }

        protected void Cancle_Click(object sender, EventArgs e)
        {

            string url = URLUtility.UrlBase + "/WebManager/Function/ExProduct/exchange/ListExchangeProducts.aspx?oid=" + ColumnID;
            Page.Response.Redirect(url);
        }

        protected void drpPName_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (drpPName.SelectedValue != "-1")
            {
                productBll = new LYQ.BLL.Product();
                LYQ.Model.Product productModel = productBll.GetModel(new Guid(drpPName.SelectedValue));

                Image1.ImageUrl = @"~\uploads\ExchangeProducts\" + productModel.SImg;
            }
            else
            {
                Image1.ImageUrl = "";
            }
        }
    }
}
